/**
 * 短信管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：短信管理页面
     创建日期：2015-09-28
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	querySMSLogs();
	 $(".date").datepicker({
	        todayBtn: "linked",
	        keyboardNavigation: false,
	        forceParse: false,
	        calendarWeeks: true,
	        autoclose: true
	    });
});

/**
 * 查询快递员站点
 * @returns
 */
var querySMSLogs = function(){
	
	var name = $('#name').val();
	var telephone = $('#telephone').val();
	var smsContent = $('#smsContent').val();
	var createTime = $('#createTime').val();
	var smsBlack = $('#selectList').val();
	
	 var datas =  {"p":"smsManage","m":"smsLogList","name":name,"telephone":telephone,"smsContent":smsContent,"createTime":createTime,"smsBlack":smsBlack};
	 var columnDefine = {"id":"ID","uid":"快递员ID","name":"快递员","telephone":"手机号","company":"快递公司","createTime":"发送日期","smsContent":"短信内容","smsBlack":"拉黑状态","phone":"接收人手机"};
	 
	 var formatters={
			 "smsBlack": function (column, row)
	            {
					 if(row.smsBlack==1)
					 {
						 return "<font color='red'>拉黑</font>";
					 }
						 else 
					 {
						 return "正常";
					 }
	            },
	            "createTime": function (column, row)
	            {
	            	var date = new Date( row.createTime*1000 );
	            	return date.getFullYear()+'-' + (date.getMonth()+1)+ '-' + date.getDate();
	            }
	 };
	 var buttons =''; //操作按钮
		buttons+='<p>';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-danger\' onclick="updateSMSBlackState();">拉黑</button>';
		buttons+='</p>';
	 hui.pager('service',datas ,"smsLogList", columnDefine , formatters , buttons ); //分页查询
	 
};

/**
 * 拉黑当前用户
 * @returns
 */
var updateSMSBlackState = function(){
	
	var datas = $('#pop_win .data').text();
	var json = eval('('+datas+')');
	var ids = json.id;
	
	var json = common.ajaxRequest('service', {"p":"smsManage","m":"updateSmsBlackState","smsBlack":1,"ids":ids} , 'GET');
	if( json && json.result == 0 )
	{
		querySMSLogs(); //更新显示状态
		$('#pop_win').modal('hide');
		common.showMsg('info', '', '设置成功!');
	}
	else
	{
		common.showMsg('error', '', '设置拉黑失败!' + json.message);
		return;
	}
};

var sendSmsParam=function(){
	var idsDom=$("#smsLogList input[name='select']:checked");
	var adActiveIds="";
	if(idsDom.length<1){
		common.showMsg('error', '', '发短信，至少要选择一条记录');
		return;
	}else if(idsDom.length==1){
		adActiveIds=idsDom.eq(0).parent().nextAll().eq(3).html();
	}else{
		if(idsDom.eq(0).val()!="all"){
			adActiveIds+=idsDom.eq(0).parent().nextAll().eq(3).html()+",";
		}
		for(var i=1;i<idsDom.length;i++){
			adActiveIds+=idsDom.eq(i).parent().nextAll().eq(3).html()+",";
		}
		adActiveIds=adActiveIds.substring(0, adActiveIds.length-1);
	}
	//alert(adActiveIds);
	//sendSms_duoxuan();
	return adActiveIds;
};

var sendSms_duoxuan=function(){
	$("#showWinOfSure").modal('show');
	$("#winOfSureText").html(sendSmsParam());
	$("#winOfSure").attr("onclick","sendSms_duoxuanSure()");
};
var sendSms_duoxuanSure=function(){
	var datas = $('#winOfSureText').html();
	var content=$("#winOfSureSMSText").val();
	/*alert(datas);
	alert(content);
	return ;*/
	var json = common.ajaxRequest('service', {"p":"smsManage","m":"sendSMSTOUser","smsContent":content,"isAll":"no","isTemplate":"","userPhones":datas} , 'POST');
	if( json && json.result == 0 )
	{
		//querySMSLogs(); //更新显示状态
		//$('#pop_win').modal('hide');
		common.showMsg('info', '', '发送成功!');
	}
	else
	{
		common.showMsg('error', '', '发送失败!' + json.message);
		return;
	}
};

var sendSms_qunfa=function(){
	$("#showWinOfSure").modal('show');
	$("#winOfSureText").html("请填写发送短信数量：<input type='text' id='showWinOfSureNum'>");
	$("#winOfSure").attr("onclick","sendSms_qunfaSure()");
}

var sendSms_qunfaSure=function(){
	var smsNum=$("#showWinOfSureNum").val();
	var content=$("#winOfSureSMSText").val();
	/*alert(smsNum);
	alert(content);
	return ;*/
	var json = common.ajaxRequest('service', {"p":"smsManage","m":"sendSMSTOUser","smsContent":content,"isAll":"yes","isTemplate":"","smsNum":smsNum} , 'POST');
	if( json && json.result == 0 )
	{
		$('#showWinOfSure').modal('hide');
		common.showMsg('info', '', '发送成功');
	}
	else
	{
		common.showMsg('error', '', '发送失败!' + json.message);
		return;
	}
}

